//
//  chatViewCell.m
//  testTVC
//
//  Created by user10 on 2017/6/19.
//  Copyright © 2017年 user10. All rights reserved.
//

#import "chatViewCell.h"
#import <Masonry.h>
#import <UIImageView+WebCache.h>
#import "AimTapGestureRecognizer.h"

#define screenWidth [UIScreen mainScreen].bounds.size.width
    
@implementation chatViewCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if ( self ) {
        _longPressGesture =  [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(LongPress:)];
        self.timeL = [[UILabel alloc]init];
        self.timeL.numberOfLines = 0;
        [self.contentView addSubview:self.timeL];
        [self.timeL mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.centerX.equalTo(self.contentView);
            make.width.mas_equalTo(screenWidth);
        }];
        self.timeL.textAlignment = NSTextAlignmentCenter;

        
        self.nameL = [[UILabel alloc]init];
        self.nameL.numberOfLines = 0;
        [self.contentView addSubview:self.nameL];
        [self.nameL mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(self.timeL.mas_bottom);
            make.right.mas_equalTo(self.contentView).offset(-15);
            make.width.mas_lessThanOrEqualTo(100);
        }];
        
        
        
        self.shopNameL = [[UILabel alloc]init];
        [self.contentView addSubview:self.shopNameL];
        [self.shopNameL mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(self.nameL);
            make.right.mas_equalTo(self.nameL.mas_left).offset(-5);
            make.left.greaterThanOrEqualTo(self.contentView).offset(15);
        }];
        
        self.imgV = [[UIImageView alloc]init];
        [self.contentView addSubview:self.imgV];
        [self.imgV mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(self.nameL.mas_bottom).offset(15);
            make.right.mas_equalTo(self.contentView).offset(-15);
            make.size.mas_equalTo(CGSizeMake(45, 45));
            make.bottom.lessThanOrEqualTo(self.contentView).offset(-15);
            
        }];
        
        self.commentL = [[UILabel alloc]init];
        self.commentL.layer.cornerRadius =5;
        self.commentL.clipsToBounds = YES;
        [self.contentView addSubview:self.commentL];
        self.commentL.numberOfLines = 0;
        [self.commentL mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(self.imgV);
            make.right.mas_equalTo(self.imgV.mas_left).offset(-10);
            make.width.mas_lessThanOrEqualTo(screenWidth - 80);
            make.bottom.lessThanOrEqualTo(self.contentView).offset(-15);
        }];
        self.commentL.backgroundColor = [UIColor colorWithRed:119/255.0 green:210/255.0 blue:58/255.0 alpha:1];
        
    }
    return self;
}


- (void)setChatModel:(chatModel *)chatModel {
    _chatModel = chatModel;
    self.timeL.text = chatModel.MSG_DATE;
    self.shopNameL.text = chatModel.MSG_SHOP_NAME;
    self.commentL.text = chatModel.MSG_DATA_TEXT;
    self.commentL.layer.cornerRadius = 5;
    self.nameL.text = chatModel.MSG_TANTO_NAME;
    
    if(chatModel.MSG_DATA_IMG.length != 0){
        [self.photo sd_setImageWithURL:[NSURL URLWithString:chatModel.MSG_DATA_IMG]];
        
        AimTapGestureRecognizer *tapGesture = [[AimTapGestureRecognizer alloc]initWithTarget:self action:@selector(tapAction:)];
        tapGesture.imgUrl = chatModel.MSG_DATA_IMG;
        [self.photo addGestureRecognizer:tapGesture];
        
    }else if(chatModel.MSG_DATA_VIDEO.length != 0){
        //        cell.photo.image = [self thumbnailImageForVideo:[NSURL URLWithString:chatModel.MSG_DATA_VIDEO] atTime:1];
        
        [self.photo sd_setImageWithURL:[NSURL URLWithString:chatModel.MSG_DATA_VIDEO] placeholderImage:[UIImage imageNamed:@"icon"]];
        
        AimTapGestureRecognizer *tapGesture = [[AimTapGestureRecognizer alloc]initWithTarget:self action:@selector(playVideo:)];
        tapGesture.videoUrl = chatModel.MSG_DATA_VIDEO;
        [self.photo addGestureRecognizer:tapGesture];
    }
}

- (void)tapAction:(AimTapGestureRecognizer *)ges{
    if (self.clickBlock) {
        self.clickBlock(ges);
    }
}
- (void)playVideo:(AimTapGestureRecognizer *)ges{
    
    if (self.playBlock) {
        self.playBlock(ges);
    }
}
- (void)playVoice:(AimTapGestureRecognizer *)ges{
    
    if (self.playVoiceBlock) {
        self.playVoiceBlock(ges);
    }
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];
    
    // Configure the view for the selected state
}

- (void)LongPress:(UIGestureRecognizer *)recognizer{

    if ([self.delegate respondsToSelector:@selector(cellLongPress:)]) {
        // 调用代理对象的刷新方法，代理对象去实现方法
        [self.delegate cellLongPress:recognizer];
    }
}


@end
